package edu.whut.mall.admin.controller;

import edu.whut.mall.admin.domain.RoleParams;
import edu.whut.mall.admin.service.ISysRoleService;
import edu.whut.mall.admin.vo.ResourceVo;
import edu.whut.mall.admin.vo.RoleVo;
import edu.whut.mall.common.api.CommonPage;
import edu.whut.mall.common.api.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @program: d9lab-mall
 * @description: 角色管理器
 * @author: Wayne
 * @create: 2020-04-17 11:18
 **/
@Slf4j
@Api(value="角色管理器",tags = {"角色管理器"})
@RestController
@RequiredArgsConstructor
@RequestMapping("/role")
public class RestRoleController {

    private final ISysRoleService sysRoleService;

    @ApiOperation("增加角色")
    @PostMapping("/addOneRole")
    public CommonResult addOneRole(@Valid @RequestBody RoleParams roleParams){
        int result = sysRoleService.insertOneRole(roleParams);
        if (result >= 0) {
            return CommonResult.success("添加角色成功");
        }
        return CommonResult.failed("资源角色失败");
    }

    @ApiOperation("更新角色")
    @PostMapping("/updateRole")
    public CommonResult updateRole(@Valid @RequestBody RoleParams roleParams){
        int result = sysRoleService.updateRole(roleParams);
        if (result >= 0) {
            return CommonResult.success("更新角色成功");
        }
        return CommonResult.failed("角色更新失败");
    }


    @ApiOperation("获取所有角色")
    @GetMapping("/getRoleList")
    public CommonResult getAdminList( @RequestParam(value="pageNum",defaultValue = "1") Integer pageNum,
                                      @RequestParam(value="size",defaultValue ="10") Integer size){
        CommonPage<RoleVo> roleVos = sysRoleService.listRoles(pageNum, size);
        if (roleVos != null) {
            return CommonResult.success(roleVos,"获取角色列表成功");
        }
        return CommonResult.failed("获取角色列表失败");
    }

    @ApiOperation("根据角色获取所有资源")
    @GetMapping("/getResourceVosByRoleId")
    public CommonResult getResourceVosByRoleId(@NotNull @RequestParam Long roleId){
        List<ResourceVo> resourceVos = sysRoleService.listResourceVosByRoleId(roleId);
        if (resourceVos != null) {
            return CommonResult.success(resourceVos,"获取角色资源成功");
        }
        return CommonResult.failed("获取角色资源失败");
    }

    @ApiOperation("根据角色id删除角色")
    @GetMapping("/deleteOneRoleByRoleId")
    public CommonResult deleteOneRoleByRoleId(@NotNull @RequestParam Long roleId){
        int deleteResult = sysRoleService.deleteOneRole(roleId);
        if (deleteResult >= 0) {
            return CommonResult.success("删除角色成功");
        }
        return CommonResult.failed("删除角色失败");
    }
}
